<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use App\Base\Annotation\Common;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ODM\Document(repositoryClass="App\Repository\SiteOilCategoryRepository")
 */
class SiteOilCategory
{
    /**
     * @Common(cnName="#",editType="text",listType="text")
     * @ODM\Id(strategy="INCREMENT")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"list"})
     */
    private $id;

    /**
     * @Common(cnName="名称",editType="text",listType="text")
     * @ODM\Field( nullable=false)
     * @ORM\Column(type="string", length=255)
     * @Groups({"list"})
     */
    private $name;

    /**
     * @Common(cnName="地址",editType="text",listType="text")
     * @ODM\ReferenceMany(targetDocument="App\Entity\SiteOil", mappedBy="category", orphanRemoval=true)
     * @ORM\OneToMany(targetEntity="App\Entity\SiteOil", mappedBy="category", orphanRemoval=true)
     * @Groups({"list"})
     */
    private $oils;

    public function __construct()
    {
        $this->oils = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|SiteOil[]
     */
    public function getOils(): Collection
    {
        return $this->oils;
    }

    public function addOil(SiteOil $oil): self
    {
        if (!$this->oils->contains($oil)) {
            $this->oils[] = $oil;
            $oil->setCategory($this);
        }

        return $this;
    }

    public function removeOil(SiteOil $oil): self
    {
        if ($this->oils->contains($oil)) {
            $this->oils->removeElement($oil);
            // set the owning side to null (unless already changed)
            if ($oil->getCategory() === $this) {
                $oil->setCategory(null);
            }
        }

        return $this;
    }
}
